package summerMain

import (
	"fmt"
	"github.com/emicklei/go-restful/v3"
	"github.com/rs/zerolog"
	"github.com/rs/zerolog/log"
	"net/http"
	"os"
	summerConfig2 "webGoDemo/summer/summerConfig"
	summerContainer2 "webGoDemo/summer/summerContainer"
	summerHepler2 "webGoDemo/summer/summerHepler"
	summerRouter2 "webGoDemo/summer/summerRouter"
)

func autoLoad() {

	// 加载日志
	log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

	// 加载数据库配置 初始化数据库
	summerConfig2.DBInit()

}

// autoInit 启动初始化
func autoInit() {
	config := summerContainer2.Get("config")
	summerConfig2.ConnectDB(config.(summerConfig2.Conf))
}

func start() {
	////开启web监听
	addr := fmt.Sprintf("%s:%d", "", 8080)
	log.Info().Str("addr", addr).Msg("http server start to listen")
	err := http.ListenAndServe(addr, nil)
	log.Fatal().Err(err).Msg("http server run over")
}

// Run 运行
func Run() {
	autoLoad()
	autoInit()
	summerHepler2.InitHelper()
	summerRouter2.AddUserRouter(restful.DefaultContainer)
	start()
}
